<?php 
namespace Admin\Controller;


class SessionController extends AdminController
{
	public function index()
	{
		// echo time();exit;
		$name = I('get.name');
		// var_dump($name);exit;
		// 实例化User对象
		$User = M('session');
		if(empty($name)){
			$count = $User->count('id');// 查询满足要求的总记录数
			$Page = new \Think\Page($count,5);// 实例化分页类 传入总记录数和每页显示的记录数(5)
			// 进行分页数据查询 注意limit方法的参数要使用Page类的属性
            $Page->setConfig('first','首页');
            $Page->setConfig('prev','上一页');
            $Page->setConfig('next','下一页');
            $Page->setConfig('last','尾页');
            $show = $Page->show();// 分页显示输出

            $data = $User->field('lamp_session.id,c.name,h.hall,m.name as mname,lamp_session.starttime,lamp_session.endtime,lamp_session.price,lamp_session.num')->join('lamp_cinema c on lamp_session.cid=c.id')->join('lamp_hall h on lamp_session.hid=h.id')->join('lamp_movie m on lamp_session.mid=m.id')->limit($Page->firstRow.','.$Page->listRows)->select();
            // var_dump($data);exit;
		}else{
			$count = $User->join('lamp_cinema c on lamp_session.cid = c.id')->where('c.name like "%'.$name.'%"')->count('lamp_session.id');// 查询满足要求的总记录数
			// echo $count;exit;
			$Page = new \Think\Page($count,5);// 实例化分页类 传入总记录数和每页显示的记录数(5)
			// 进行分页数据查询 注意limit方法的参数要使用Page类的属性
			// $data = $User->field('lamp_hall.id,hall,seat,c.name')->join('lamp_cinema c on lamp_hall.cid = c.id')->order('lamp_hall.cid')->where('c.name like "%'.$name.'%"')->limit($Page->firstRow.','.$Page->listRows)->select();
			// $sql = "select s.id,c.name,h.hall,m.name as mname,s.starttime,s.endtime,s.price,s.num from lamp_session s join lamp_cinema c on s.cid=c.id join lamp_hall h on s.hid = h.id join lamp_movie m on s.mid=m.id where c.name like '%".$name."%'";
			// $data = M()->query($sql);
            $Page->setConfig('first','首页');
            $Page->setConfig('prev','上一页');
            $Page->setConfig('next','下一页');
            $Page->setConfig('last','尾页');
            $map['name'] = $name;
            // var_dump($map);
            $Page->parameter=$map;
            $show = $Page->show();// 分页显示输出
            $data = $User->field('lamp_session.id,c.name,h.hall,m.name as mname,lamp_session.starttime,lamp_session.endtime,lamp_session.price,lamp_session.num')->join('lamp_cinema c on lamp_session.cid=c.id')->join('lamp_hall h on lamp_session.hid=h.id')->join('lamp_movie m on lamp_session.mid=m.id')->limit($Page->firstRow.','.$Page->listRows)->where('c.name like "%'.$name.'%"')->select();
			// var_dump($data);exit;
		}

		
		// var_dump($data);exit;
		$this->assign('list',$data);// 赋值数据集
		$this->assign('page',$show);// 赋值分页输出
		// echo time();exit;
		$this->assign('title','影厅列表');
		$this->display();
	}
	// 删除场次
    public function del()
    {
    	//接收参数
    	$id = I('get.id/d');
    	
    	if(empty($id)){
    		$this->redirect('Session/index');
    		exit;
    	}
    	// echo $id;exit;
    	if(M('session')->delete($id) > 0){
    		$this->success('恭喜您删除成功',U('index'));
    	}else{
    		$this->error('删除失败',U('index'));
    	}
    }
    //添加页
    public function add()
    {
    	$this->assign('title','添加场次');
    	$this->display();
    }
    public function insert()
    {
    	// var_dump($_POST);exit;
    	$data = $_POST;
    	// $list = M('cinema')->field('id')->where('name="'.$data['name'].'"')->select();
    	// var_dump($list);
    	if(!$list = M('cinema')->field('id')->where('name="'.$data['name'].'"')->select()){
    		$this->error('影院不存在,请确认影院');
    	}
    	// var_dump($list);exit;
    	$data['cid'] = $list[0]['id']; 
    	if(!$list = M('hall')->field('id')->where('cid="'.$data['cid'].'" and hall="'.$data['hall'].'"')->select()){
    		$this->error('该影院没有该影厅');
    	}
    	// var_dump($list);exit;
    	$data['hid'] = $list[0]['id'];
    	if(!$list = M('movie')->field('id')->where('name="'.$data['mname'].'"')->select()){
    		$this->error('未查到电影信息,请确认电影名');
    	}
    	$data['mid'] = $list[0]['id'];
    	if(strtotime($data['starttime']) < time()){
    		$this->error('时间不合法,请再次确认');
    	}
    	$data['starttime'] = strtotime($data['starttime']);
    	if(strtotime($data['endtime']) < $data['starttime']){
    		$this->error('结束时间小于开始时间,你特么逗我玩吗');
    	}
    	$data['endtime'] = strtotime($data['endtime']);
    	$list = M('hall')->field('seat')->where('id='.$data['hid'])->select();
    	if($list[0]['seat'] < $data['num']){
    		$this->error('多出的人坐哪？你是猪吗?');
    	}
    	$session = D('session');
    	if (!$session->create($data)) {
    		$this->error($session->getError());
    	} else {
    		// var_dump($session->fetchSql()->add());exit;
    		//自动验证成功
    		if($session->add() > 0){
    			$this->success('添加成功',U('index'));
	    	}else{
	    		$this->error('添加失败');
	    	}
    	}
    }
    public function edit()
    {
    	$id = I('get.id');
    	$sql = "select s.id,c.name,h.hall,m.name as mname,s.starttime,s.endtime,s.price,s.num from lamp_session s join lamp_cinema c on s.cid=c.id join lamp_hall h on s.hid = h.id join lamp_movie m on s.mid=m.id where s.id=".$id;
		$data = M()->query($sql);
		// echo date('Y/m/d H:i',$data[0]['starttime']);
		// var_dump($data);exit;
		$this->assign('list',$data[0]);
    	$this->assign('title','编辑页');
    	$this->display();
    }
    public function update()
    {
    	$id = I('get.id/d');
    	// var_dump($id);
    	// var_dump($_POST);exit;
    	$data = $_POST;
    	// $list = M('cinema')->field('id')->where('name="'.$data['name'].'"')->select();
    	// var_dump($list);
    	if(!$list = M('cinema')->field('id')->where('name="'.$data['name'].'"')->select()){
    		$this->error('影院不存在,请确认影院');
    	}
    	// var_dump($list);exit;
    	$data['cid'] = $list[0]['id']; 
    	if(!$list = M('hall')->field('id')->where('cid="'.$data['cid'].'" and hall="'.$data['hall'].'"')->select()){
    		$this->error('该影院没有该影厅');
    	}
    	// var_dump($list);exit;
    	$data['hid'] = $list[0]['id'];
    	if(!$list = M('movie')->field('id')->where('name="'.$data['mname'].'"')->select()){
    		$this->error('未查到电影信息,请确认电影名');
    	}
    	$data['mid'] = $list[0]['id'];
    	if(strtotime($data['starttime']) < time()){
    		$this->error('时间不合法,请再次确认');
    	}
    	$data['starttime'] = strtotime($data['starttime']);
    	if(strtotime($data['endtime']) < $data['starttime']){
    		$this->error('结束时间小于开始时间,你特么逗我玩吗');
    	}
    	$data['endtime'] = strtotime($data['endtime']);
    	$list = M('hall')->field('seat')->where('id='.$data['hid'])->select();
    	if($list[0]['seat'] < $data['num']){
    		$this->error('多出的人坐哪？你是猪吗?');
    	}
    	$session = D('session');
    	if (!$session->create($data)) {
    		$this->error($session->getError());
    	} else {
    		// echo $session->where('id='.$id)->fetchSql()->save();exit;
    		//自动验证成功
    		if($session->where('id='.$id)->save()){
    			$this->success('编辑成功',U('index'));
	    	}else{
	    		$this->error('编辑失败',U('edit?id='.$id));
	    	}
    	}
    }
}
























